package com.sky.controller.admin;


import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.exception.SqlException;
import com.sky.pojo.StatusUpdate;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.server.EmployeeServer;
import com.sky.vo.EmployeeLoginVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Autowired
    private EmployeeServer employeeServerImpl;


    /**
     * 用户登陆
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {

        EmployeeLoginVO employeeLoginVO = employeeServerImpl.login(employeeLoginDTO);
        return Result.success(employeeLoginVO);
    }

    /**
     * 添加员工
     */
    @PostMapping()
    public Result addEmployee(@RequestBody EmployeeDTO employeeDTO) {
        // todo 进行dto值的判断

        employeeServerImpl.addEmployee(employeeDTO);
        return Result.success();
    }


    /**
     * 根据id查询用户
     */
    @GetMapping("/{id}")
    public Result<Employee> selectEmployeeById(@PathVariable Long id) {
        Employee employeeOne = employeeServerImpl.selectEmployeeById(id);
        return Result.success(employeeOne);
    }

    /**
     * 更新员工状态
     */
    @PutMapping
    public Result updateEmployee(@RequestBody EmployeeDTO employeeDTO) {
        // Optional.ofNullable(employeeDTO).map(e -> {e.getId();e.getName();e.getUsername();e.getPhone();e.getSex();e.getIdNumber();return null;}).orElseGet(()->"空");

        employeeServerImpl.updateEmployeeById(employeeDTO);
        return Result.success();
    }


    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result<PageResult> pageEmployee(Integer page, Integer pageSize, String name) {
        EmployeePageQueryDTO employeePageQueryDTO = new EmployeePageQueryDTO(page,pageSize,name);
        PageResult pageResult = employeeServerImpl.pageEmployee(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 修改员工状态
     */
    @PostMapping("/status/{status}")
    public Result status(@PathVariable Integer status, Long id) {
        employeeServerImpl.status(status, id);
        return Result.success();
    }

    /**
     * 修改员工密码
     */
    @PutMapping("/editPassword")
    public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO){
        employeeServerImpl.editPassword(passwordEditDTO);
        return Result.success();
    }

}
